package com.ip.getip;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import com.gargoylesoftware.htmlunit.util.Cookie;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.*;
import java.util.logging.Level;

/**
 * @Description TODO
 * @Author leixin
 * @Date 2019/9/28 下午9:03
 * @Version 1.0
 **/
public class aaa {
    public static void main(String[] args) throws Exception {
//        Logger.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(Level.OFF);

        // 模拟一个浏览器
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        // 设置webClient的相关参数
        webClient.setCssErrorHandler(new SilentCssErrorHandler());
        //设置ajax
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        //设置支持js
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.waitForBackgroundJavaScript(6000);
        //CSS渲染禁止
        webClient.getOptions().setCssEnabled(false);
        //超时时间
        webClient.getOptions().setTimeout(5000);
        //设置js抛出异常:false
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        //允许重定向
        webClient.getOptions().setRedirectEnabled(true);
        //允许cookie
        webClient.getCookieManager().setCookiesEnabled(true);
        // 模拟浏览器打开一个目标网址
        HtmlPage page = webClient.getPage("https://passport.liepin.com/account/v1/hlogin?backurl=https://h.liepin.com/#sfrom=click-pc_homepage-front_navigation-hunter_new");

        webClient.waitForBackgroundJavaScript(5000);

        HtmlForm form = page.getForms().get(1);//page.getFormByName("");
        HtmlTextInput txtUName = (HtmlTextInput) form.getInputByName("login"); //用户名text框
        txtUName.setValueAttribute("15008425044");
        HtmlPasswordInput txtPwd = (HtmlPasswordInput) form.getInputByName("pwd");//密码框
        txtPwd.setValueAttribute("lx17451745");
        HtmlSubmitInput submit = (HtmlSubmitInput) form.getInputByName("tijiao");
        HtmlPage login = submit.click();
        // 等待JS驱动dom完成获得还原后的网页
        webClient.waitForBackgroundJavaScript(2000);
        //获取cookie
        Set<Cookie> cookies = webClient.getCookieManager().getCookies();
        Map<String, String> responseCookies = new HashMap<String, String>();
        //组装cookie
        StringBuilder cookie = new StringBuilder();
        for (Cookie c : cookies) {
            responseCookies.put(c.getName(), c.getValue());
            cookie.append(c.getName() + "=" + c.getValue() + ";");
        }
        //找人页面
        DomElement element = login.getFirstByXPath("/html/body/div[1]/header/div/div/ul/li[3]/a");
        HtmlPage findSomeone = element.click();
        //关键字输入
        HtmlForm findForm = findSomeone.getForms().get(1);//page.getFormByName("");
        HtmlTextInput keys = (HtmlTextInput) findForm.getInputByName("keys"); //关键字
        //关键字的值
        keys.setValueAttribute("医疗");
        //获取搜索按钮
        DomElement someone = findSomeone.getFirstByXPath("/html/body/div[2]/div[2]/div[1]/div[2]/form/table/tfoot/tr[2]/td/button");
        List href = new ArrayList();
        HtmlPage resume = someone.click();
//        hrefList(href,someone.click());
        DomElement pageSize = resume.getAnchorByText("下页");
        HtmlPage aa = pageSize.click();

        System.out.println(aa.asXml());
//        String s = doGet(href.get(0).toString(), cookie.toString());
//        System.out.println("内容"+s);
    }

    public static String doGet(String url, String cookie) throws Exception {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 通过址默认配置创建一个httpClient实例
            httpClient = HttpClients.createDefault();
            // 创建httpGet远程连接实例
            HttpGet httpGet = new HttpGet(url);
            // 设置请求头信息，鉴权
            httpGet.setHeader("Cookie", cookie);
            // 设置配置请求参数
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                    .setConnectionRequestTimeout(35000)// 请求超时时间
                    .setSocketTimeout(60000)// 数据读取超时时间
                    .build();
            // 为httpGet实例设置配置
            httpGet.setConfig(requestConfig);
            // 执行get请求得到返回对象
            response = httpClient.execute(httpGet);
            // 通过返回对象获取返回数据
            HttpEntity entity = response.getEntity();
            // 通过EntityUtils中的toString方法将结果转换为字符串
            result = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static void hrefList(List href, HtmlPage resume) throws IOException {
        //简历页面,第一页
        Document parse = Jsoup.parse(resume.asXml());
        List<Element> infoListEle = parse.getElementsByAttributeValue("class", "card-portrait");
        infoListEle.forEach(i -> {
            href.add("https://h.liepin.com" + i.select("a").attr("href"));
        });
        //获取分页数据链接
        DomElement pageSize = resume.getAnchorByText("下页");

        hrefList(href, pageSize.click());
        if (pageSize.click().getUrl().equals(resume.getUrl())) {
            return;
        }
    }
}
